﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Adams.Public.Web.Controllers
{
    [EnsureSsl]
    public class SecurityController : Controller
    {
        [AppendLayoutViewModel]
        public ActionResult Login()
        {
            var model = Business.Web.Security.Login.PageLoad();
            return View(model);
        }

        public JsonResult TryLogin(Adams.Public.DataTransfer.Security.LoginInput input)
        {
            var model = Adams.Public.Web.Business.Security.TryLogin(input);

            if (model.IsValid)
            {
                FormsAuthentication.SetAuthCookie(model.Token, input.RememberMe.ToBool());
            }

            return Json(model);
        }

        public ActionResult Logout()
        {
            System.Web.Security.FormsAuthentication.SignOut();
            return Redirect("/");
        }

        [AppendLayoutViewModel]
        public ActionResult Forgot(Adams.Public.DataTransfer.Web.Security.Forgot.PageLoadInput input)
        {
            var model = Business.Web.Security.Forgot.PageLoad(input);
            return View(model);
        }

        public JsonResult TryForgot(string captcha, Adams.Public.DataTransfer.Security.ForgotInput input)
        {
            string code = Request.Cookies["code"].Value;
            Request.Cookies["code"].Expires = DateTime.Now.AddDays(-1);

            if (Business.Security.ValidateCaptcha(captcha, code))
            {
                var model = Business.Security.TryForgot(input);
                return Json(model);
            }
            else
            {
                var model = new DataTransfer.FormOutput();
                model.InvalidFields.Add(new DataTransfer.InvalidField() { Code = 26, Name = "Captcha" });
                return Json(model);
            }
        }

        [AppendLayoutViewModel]
        public ActionResult ForgotComplete(Adams.Public.DataTransfer.Web.Security.ForgotComplete.PageLoadInput input)
        {
            var model = Business.Web.Security.ForgotComplete.PageLoad(input);
            return View(model);
        }

        [AppendLayoutViewModel]
        public ActionResult Signup()
        {
            var model = Business.Web.Security.Signup.PageLoad();
            return View(model);
        }

        public JsonResult TrySignup(string captcha, Adams.Public.DataTransfer.Security.SignupInput input)
        {
            string code = Request.Cookies["code"].Value;
            Request.Cookies["code"].Expires = DateTime.Now.AddDays(-1);

            if (Business.Security.ValidateCaptcha(captcha, code))
            {
                var model = Business.Security.TrySignup(input);
                return Json(model);
            }
            else
            {
                var model = new DataTransfer.FormOutput();
                model.InvalidFields.Add(new DataTransfer.InvalidField() { Code = 26, Name = "Captcha" });
                return Json(model);
            }
        }

        [AppendLayoutViewModel]
        public ActionResult SignupComplete(Adams.Public.DataTransfer.Web.Security.SignupComplete.PageLoadInput input)
        {
            var model = Business.Web.Security.SignupComplete.PageLoad(input);
            return View(model);
        }
        
        [AppendLayoutViewModel]
        public ActionResult ConfirmEmail(DataTransfer.Web.Security.ConfirmEmail.PageLoadInput input)
        {
            var model = Business.Web.Security.ConfirmEmail.PageLoad(input);
            return View(model);
        }

        [AppendLayoutViewModel]
        public ActionResult PasswordReset(Adams.Public.DataTransfer.Web.Security.PasswordReset.PageLoadInput input)
        {
            var model = Business.Web.Security.PasswordReset.PageLoad(input);
            return View(model);
        }

        public JsonResult TryPasswordReset(Adams.Public.DataTransfer.Security.PasswordResetInput input)
        {
            var model = Business.Security.TryPasswordReset(input);
            return Json(model);
        }

        [AppendLayoutViewModel]
        public ActionResult Resend(Adams.Public.DataTransfer.Web.Security.Resend.PageLoadInput input)
        {
            var model = Business.Web.Security.Resend.PageLoad(input);
            return View(model);
        }

        public JsonResult TryResend(string captcha, Adams.Public.DataTransfer.Security.ResendInput input)
        {
            string code = Request.Cookies["code"].Value;
            Request.Cookies["code"].Expires = DateTime.Now.AddDays(-1);

            if (Business.Security.ValidateCaptcha(captcha, code))
            {
                var model = Business.Security.TryResend(input);
                return Json(model);
            }
            else
            {
                var model = new DataTransfer.FormOutput();
                model.InvalidFields.Add(new DataTransfer.InvalidField() { Code = 26, Name = "Captcha" });
                return Json(model);
            }
        }

        [AppendLayoutViewModel]
        public ActionResult ResendComplete(Adams.Public.DataTransfer.Web.Security.ResendComplete.PageLoadInput input)
        {
            var model = Business.Web.Security.ResendComplete.PageLoad(input);
            return View(model);
        }

        public CaptchaResult Captcha()
        {
            string randomText = Shared.Str.GenerateRandomString(6);
            var code = Business.Security.GenerateCaptcha(randomText);
            HttpCookie hc = new HttpCookie("code", code);
            hc.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(hc);

            return new CaptchaResult(randomText);
        }

    }
}
